/*
 * =====================================================================================
 *
 *       Filename:  mcuserver.cpp
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  04/16/2015 01:49:59 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Dellinger.Zhang (mn), rainxy2000@hotmail.com
 *        Company:  TD studio
 *
 * =====================================================================================
 */
#include "mcuserver.h"
#include "SuperCommandIn.h"

void
MCUServer::newTCPTask(const int sock, const struct sockaddr_in* addr)
{
	Zebra::logger->debug("%s", __PRETTY_FUNCTION__);
}

bool
MCUServer::init()
{
	pool_ = new ctTCPTaskPool(5, 1);
	if (NULL == pool_
			|| !pool_->init())
	{
		Zebra::logger->error("fail to init task pool!");
		return false;
	}

	setIPInner(ctSocket::getIPByIfName(Zebra::global["ifname"].c_str()));
	if (false == ctSubNetService::init())
	{
		Zebra::logger->error("ctSubNetService::init failed!");
		return false;
	}
	return true;
}

bool MCUServer::msgParse_SuperService(
		const Cmd::t_NullCmd* pNullCmd, const DWORD nCmdLen)
{
	bool ret = false;
	Zebra::logger->debug("%s", __PRETTY_FUNCTION__); 
	using namespace Cmd::Super;
	switch(pNullCmd->wCmd)
	{
		case CMDIN_STARTUP:
			Zebra::logger->info("CMDIN_STARTUP: %u", pNullCmd->wSubCmd);
			ret = true;
			break;
		case CMDIN_ROOMMEDIA:
			ret = msgParse_MediaRecord(pNullCmd, nCmdLen);
			break;
		default:
			break;
	}
	return ret;
}


bool MCUServer::msgParse_MediaRecord(
		const Cmd::t_NullCmd* cmd, const DWORD cmdlen)
{
	bool ret = false;
	using namespace Cmd::Super;
	switch(cmd->para)
	{
		case SCMDIN_MEDIARECORD_SCHEDULE_RECORD:
			{
				const stMediaRecordStartRecord *rev = 
					static_cast< const stMediaRecordStartRecord * >(cmd);
				//TODO: scheduling

				stMediaRecordStartRecordResponse rep;
				rep.record_id	= rev->record_id;
				rep.ret_code	= 0;

				sendCmdToSuperServer(&rep, sizeof(rep));
				ret = true;
			}
			break;
		case SCMDIN_MEDIARECORD_DEPLOY_NOTIFICATION:
			{
				const stMediaRecordRoomDeployNotification *rev = 
					static_cast< const stMediaRecordRoomDeployNotification * >(cmd);
				//TODO: deploy

				Cmd::Super::stMediaRecordRoomDeployNotificationAck rep;
				rep.recording_id	= rev->recording_id;
				rep.room_id			= rev->room_id;
				rep.srv_id			= rev->srv_id;
				sendCmdToSuperServer(&rep, sizeof(rep));

				ret = true;
			}
			break;
		default:
			break;
	}
	return ret;
}

